<template>
  <div class="page-container">
    <!-- 面包屑部分 -->
    <div class="breadcrumb">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item>会员中心</el-breadcrumb-item>
        <el-breadcrumb-item>VIP会员管理</el-breadcrumb-item>
        <el-breadcrumb-item>VIP权益设置</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <!--工具栏-->
    <div class="toolbar">
      <el-form :inline="true" :model="filters" :size="size" ref="filters" >
        <el-row class="search-input">
          <el-form-item style="margin-bottom:0 !important">
          <el-form-item label="创建时间：" prop="startTime"  style="margin-right:0 !important">
              <el-date-picker
                v-model="filters.startTime"
                type="datetime"
                placeholder="开始时间"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="yyyy-MM-dd HH:mm:ss"
                clearable
                 @change="changeStartDate"
                :picker-options="startDateDisabled"
              ></el-date-picker>
            </el-form-item>
            <span class="line">-</span>
            <el-form-item prop="endTime">
              <el-date-picker
                v-model="filters.endTime"
                type="datetime"
                placeholder="结束时间"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="yyyy-MM-dd HH:mm:ss"
                clearable
                @change="changeEndDate"
                :picker-options="endDateDisabled"
              ></el-date-picker>
            </el-form-item>
            </el-form-item>
          <el-form-item label="VIP名称：">
            <el-input v-model.trim="filters.vipName" placeholder="输入VIP名称" clearable maxlength="80"></el-input>
          </el-form-item>
        </el-row>
        
        <el-row class="search-button-noborder" style="text-align: right">
          <el-form-item>
            <kt-button icon="el-icon-search" :label="'搜索'" type="primary" @click="findPageClick(null)" :size="size"/>
          </el-form-item>
          <el-form-item>
            <kt-button :label="$t('action.reset')" type="primary" @click="resetForm('filters')" :size="size" :plain="true"/>
          </el-form-item>
        </el-row>
      </el-form>
    </div>
    <div class="toolbar" style="float:right;padding-top:10px;padding-right:15px;">
      <table-column-filter-dialog
        ref="tableColumnFilterDialog"
        :columns="columns"
        @handleFilterColumns="handleFilterColumns"
      ></table-column-filter-dialog>
    </div>
    <div style="width:100%">
          <!--表格-->
          <el-table
            :data="tableList"
            :header-cell-style="{ 'color': '#FFF','background-color': '#409EFF','text-align': 'center'}"
            :highlight-current-row="true"
            :max-height="1500"
            :size="'mini'"
            class="specialTable"
            v-loading="loading"
          >
            <el-table-column
              type="selection"
              width="55">
            </el-table-column>
            <el-table-column align="center" label="编号" width="160" element-loading-text>
              <template slot-scope="scope">
                <span>{{ scope.row.id}}</span>
              </template>
            </el-table-column>

            <el-table-column  align="center" label="VIP名称">
              <template slot-scope="scope">
                <span>{{ scope.row.vipName || '-'}}</span>
              </template>
            </el-table-column>
            <el-table-column  align="center" label="所属组织" v-if="deptType==0 || deptType==1">
              <template slot-scope="scope">
                <span>{{ scope.row.deptName || '-'}}</span>
              </template>
            </el-table-column>
            <el-table-column  align="center" label="组织层级" v-if="deptType==0 || deptType==1">
              <template slot-scope="scope">
                <span v-if="scope.row.deptType == 0">母平台 </span>
                <span v-if="scope.row.deptType == 2">商家 </span>
                <span v-if="scope.row.deptType == 3">店铺 </span>
              </template>
            </el-table-column>
            <el-table-column  align="center" label="权益数">
              <template slot-scope="scope">
                <span>{{ scope.row.roleTotal || '-'}}</span>
              </template>
            </el-table-column>
            <el-table-column  align="center" label="创建时间">
              <template slot-scope="scope">
                 <span>{{ scope.row.createTime || '-'}}</span>
              </template>
            </el-table-column>

            <el-table-column class-name="status-col" align="left"  label="操作" width="253" >
              <template slot-scope="scope">
                <kt-button
                  @click="handleConfiguration(scope.row)" type="text" size="mini" style label="配置权益" >
                </kt-button>
              </template>
            </el-table-column>
          </el-table>
          <div class="toolbar" style="padding:10px;">
            <el-pagination
              layout="total, sizes, prev, pager, next, jumper"
              @current-change="currentChange"
              :current-page="filters.start"
              :page-size="filters.limit"
              :total="totalSize"
              style="float:right;"
              :page-sizes="[10, 20, 30, 40]"
              @size-change="sizeChange"
            ></el-pagination>
          </div>
        </div>
  </div>
</template>

<script>
  import {exportExcel} from "@/utils/excel";
  import CyTable from "@/views/Core/JicTable";
  import KtButton from "@/views/Core/KtButton";
  import {format} from "@/utils/datetime";
  import TableColumnFilterDialog from "@/views/Core/TableColumnFilterDialog";
  import {validatePhone,validateContacts,isEmail,isNumber} from '@/utils/validate'
  import XLSX from "xlsx";
  export default {
    components: {
      CyTable,
      KtButton,
      TableColumnFilterDialog,
    },
    data() { 
      return {
        startDateDisabled: {},//开始时间限制
        endDateDisabled: {},//结束时间限制
        loading:false,
        tableList:[],
        totalSize:0,
        childId:'',
        data:[],
        deptType:localStorage.getItem("deptType"),//当前登录用户所属组织类型
        size: "mini",
        filters: { 
          start:1,
          limit:10,
          startTime:null,
          endTime:null, 
          childId:null,
          vipName:null, 
          merchantOrStoreName:null,       
        },
        columns:[
          {prop: "id", label: "编号", minWidth: 90},
          {prop: "vipName", label: "VIP名称", minWidth: 120},
          {prop: "deptName", label: "所属组织", minWidth: 120},
          {prop: "deptType", label: "组织层级", minWidth: 120,formatter:this.typeFormatter},
          {prop: "roleTotal", label: "权益数", minWidth: 120},
          {prop: "createTime", label: "创建时间", minWidth: 120}, 

          ],
        filterColumns: [],
        rulePowerVisible:false,
        reviewForm: {
          reviewReason: "",
          status: "",
          childId:null,
        },
      };
    },
    methods: {
      changeStartDate: function() {
      var this_ = this;
      this_.endDateDisabled = {
        disabledDate: time => {
          if (this_.filters.startTime) {
            return time.getTime() <= new Date(this_.filters.startTime).getTime();
          }
        }
      };
    },
    changeEndDate: function() {
      var this_ = this;
      this_.startDateDisabled = {
        disabledDate: time => {
          if (this_.filters.endTime) {
            return time.getTime() >= new Date(this_.filters.endTime).getTime();
          }
        }
      };
    },
      // 换页刷新
      currentChange(pageNum) {
        this.filters.start = pageNum;
        this.findPage();
      },
      sizeChange(pageSize) {
        this.filters.start = 1;
        this.filters.limit = pageSize;
        this.findPage();
      },
      //配置权限弹框
      handleConfiguration(data){
        this.$router.push({path:"/PowerVipTemplate" , query:{"id":data.id}});
      },
      returnRuleList: function() {
      this.rulePowerVisible = false;
      this.ruleConsumeVisible = false;
     },
     //组织类型formatter
      typeFormatter:function(row, column, cellValue, index){
        if(cellValue == 0){
          return '母平台'
        }else if(cellValue == 1){
          return '子平台'
        }else if(cellValue == 2){
          return '商家'
        }else if(cellValue == 3){
          return '店铺'
        }
      },
       //点击搜索，页码重置
      findPageClick(){
        this.filters.start = 1;
        this.findPage();
      },
      // 获取分页数据
      findPage: function(data) {
        let this_ = this;
        this.loading=true;
        this.utils.request.queryMevipRuleList(this_.filters, function (res) {
          if (res.code == "0000") {
             this_.loading=false;
            if(res.data.rows.length>0){
              this_.childId=res.data.rows[0].deptId;
            }
              this_.tableList = res.data.rows;
              this_.totalSize = res.data.total;
          }
        });
      },
      // 时间格式化
      dateFormat: function (row, column, cellValue, index) {
        return format(row[column.property]);
      },

      resetForm(formName) {
        //需要校验的key
        let keys = ["startTime","endTime","childId","vipName","merchantOrStoreName"]
        let boo = this.seachTableUtils.checkFromIsNull(this.filters,keys)
        if(!boo){
          return
        }
        if (this.$refs[formName] != undefined) {
          this.$refs[formName].resetFields();
          this.filters={
            start:1,
            limit:10,
            startTime:null,
            endTime:null, 
            childId:null,
            vipName:null,
            merchantOrStoreName:null,  
          };
          this.findPage(null);
        }
      },

      // 处理表格列过滤显示
      displayFilterColumnsDialog: function () {
        this.$refs.tableColumnFilterDialog.setDialogVisible(true);
      },
      // 处理表格列过滤显示
      handleFilterColumns: function (data) {
        this.filterColumns = data.filterColumns;
        this.$refs.tableColumnFilterDialog.setDialogVisible(false);
      },
      // 处理表格列过滤显示
      initColumns: function () {
        var temp = [];
        $.each(this.columns, function (key, val) {
          temp.push(val);
        });
        this.filterColumns = temp;
      },  
    },
    mounted() {
      this.findPage();
    }
  };
</script>

<style scoped lang='scss'>
/deep/ .el-transfer-panel{
   width: 250px !important;
}
.search-input{
  .times {
    .times-to {
      padding: 0 10px;
      height: 32px;
      line-height: 32px;
      color: #999;
      display: inline-block;
    }
    /deep/ .el-input{
      width:auto !important;
      /deep/ .el-input__inner{
        width: auto !important;
      }
    }
  }
} 
</style>
